{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting MNIST_data\\train-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data\\train-labels-idx1-ubyte.gz\n",
      "Extracting MNIST_data\\t10k-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data\\t10k-labels-idx1-ubyte.gz\n",
      "in iteration the update is 0.010the accuracy is : 0.643\n",
      "in iteration the update is 0.00951the accuracy is : 0.835\n",
      "in iteration the update is 0.0090252the accuracy is : 0.8645\n",
      "in iteration the update is 0.008573753the accuracy is : 0.8798\n",
      "in iteration the update is 0.0081450624the accuracy is : 0.8848\n",
      "in iteration the update is 0.0077378095the accuracy is : 0.8953\n",
      "in iteration the update is 0.00735091846the accuracy is : 0.8968\n",
      "in iteration the update is 0.00698337247the accuracy is : 0.9012\n",
      "in iteration the update is 0.00663420378the accuracy is : 0.9043\n",
      "in iteration the update is 0.00630249369the accuracy is : 0.9075\n",
      "in iteration the update is 0.00598736910the accuracy is : 0.9066\n",
      "in iteration the update is 0.005688000511the accuracy is : 0.9113\n",
      "in iteration the update is 0.005403612the accuracy is : 0.9126\n",
      "in iteration the update is 0.0051334213the accuracy is : 0.9128\n",
      "in iteration the update is 0.00487674914the accuracy is : 0.9125\n",
      "in iteration the update is 0.004632911615the accuracy is : 0.915\n",
      "in iteration the update is 0.00440126616the accuracy is : 0.9172\n",
      "in iteration the update is 0.00418120317the accuracy is : 0.9166\n",
      "in iteration the update is 0.00397214318the accuracy is : 0.916\n",
      "in iteration the update is 0.003773535819the accuracy is : 0.9181\n",
      "in iteration the update is 0.00358485920the accuracy is : 0.9196\n",
      "in iteration the update is 0.00340561621the accuracy is : 0.9177\n",
      "in iteration the update is 0.00323533522the accuracy is : 0.9213\n",
      "in iteration the update is 0.003073568223the accuracy is : 0.9209\n",
      "in iteration the update is 0.002919889824the accuracy is : 0.9205\n",
      "in iteration the update is 0.002773895225the accuracy is : 0.9223\n",
      "in iteration the update is 0.002635200326the accuracy is : 0.9215\n",
      "in iteration the update is 0.002503440227the accuracy is : 0.9225\n",
      "in iteration the update is 0.002378268228the accuracy is : 0.9227\n",
      "in iteration the update is 0.002259354829the accuracy is : 0.9212\n",
      "in iteration the update is 0.00214638730the accuracy is : 0.9214\n",
      "in iteration the update is 0.002039067731the accuracy is : 0.9195\n",
      "in iteration the update is 0.001937114332the accuracy is : 0.9226\n",
      "in iteration the update is 0.001840258633the accuracy is : 0.922\n",
      "in iteration the update is 0.001748245734the accuracy is : 0.9232\n",
      "in iteration the update is 0.001660833435the accuracy is : 0.9224\n",
      "in iteration the update is 0.001577791636the accuracy is : 0.9242\n",
      "in iteration the update is 0.00149890237the accuracy is : 0.9239\n",
      "in iteration the update is 0.001423956838the accuracy is : 0.9256\n",
      "in iteration the update is 0.00135275939the accuracy is : 0.9273\n",
      "in iteration the update is 0.00128512140the accuracy is : 0.9247\n",
      "in iteration the update is 0.00122086541the accuracy is : 0.9252\n",
      "in iteration the update is 0.001159821642the accuracy is : 0.9244\n",
      "in iteration the update is 0.001101830543the accuracy is : 0.926\n",
      "in iteration the update is 0.001046738944the accuracy is : 0.9255\n",
      "in iteration the update is 0.00099440245the accuracy is : 0.925\n",
      "in iteration the update is 0.000944681946the accuracy is : 0.9252\n",
      "in iteration the update is 0.0008974477547the accuracy is : 0.9249\n",
      "in iteration the update is 0.0008525753548the accuracy is : 0.9257\n",
      "in iteration the update is 0.0008099465649the accuracy is : 0.927\n",
      "in iteration the update is 0.000769449250the accuracy is : 0.9259\n",
      "in iteration the update is 0.0007309767551the accuracy is : 0.927\n",
      "in iteration the update is 0.0006944279352the accuracy is : 0.9284\n",
      "in iteration the update is 0.0006597065453the accuracy is : 0.9243\n",
      "in iteration the update is 0.000626721254the accuracy is : 0.927\n",
      "in iteration the update is 0.000595385155the accuracy is : 0.9276\n",
      "in iteration the update is 0.0005656158356the accuracy is : 0.9267\n",
      "in iteration the update is 0.0005373350657the accuracy is : 0.9275\n",
      "in iteration the update is 0.000510468358the accuracy is : 0.928\n",
      "in iteration the update is 0.000484944959the accuracy is : 0.9265\n",
      "in iteration the update is 0.0004606976660the accuracy is : 0.9239\n",
      "in iteration the update is 0.0004376627661the accuracy is : 0.9284\n",
      "in iteration the update is 0.0004157796262the accuracy is : 0.9267\n",
      "in iteration the update is 0.0003949906463the accuracy is : 0.9279\n",
      "in iteration the update is 0.000375241164the accuracy is : 0.9257\n",
      "in iteration the update is 0.0003564790565the accuracy is : 0.927\n",
      "in iteration the update is 0.000338655166the accuracy is : 0.9265\n",
      "in iteration the update is 0.0003217223667the accuracy is : 0.9271\n",
      "in iteration the update is 0.0003056362368the accuracy is : 0.9279\n",
      "in iteration the update is 0.0002903544269the accuracy is : 0.9266\n",
      "in iteration the update is 0.000275836770the accuracy is : 0.9274\n",
      "in iteration the update is 0.0002620448671the accuracy is : 0.927\n",
      "in iteration the update is 0.000248942672the accuracy is : 0.9284\n",
      "in iteration the update is 0.0002364954773the accuracy is : 0.9268\n",
      "in iteration the update is 0.000224670774the accuracy is : 0.9264\n",
      "in iteration the update is 0.0002134371675the accuracy is : 0.9264\n",
      "in iteration the update is 0.000202765376the accuracy is : 0.9257\n",
      "in iteration the update is 0.0001926270377the accuracy is : 0.9267\n",
      "in iteration the update is 0.0001829956778the accuracy is : 0.9283\n",
      "in iteration the update is 0.000173845979the accuracy is : 0.9277\n",
      "in iteration the update is 0.000165153680the accuracy is : 0.9271\n",
      "in iteration the update is 0.0001568959181the accuracy is : 0.9289\n",
      "in iteration the update is 0.0001490511282the accuracy is : 0.9273\n",
      "in iteration the update is 0.0001415985683the accuracy is : 0.9279\n",
      "in iteration the update is 0.0001345186384the accuracy is : 0.9294\n",
      "in iteration the update is 0.000127792785the accuracy is : 0.9269\n",
      "in iteration the update is 0.00012140305686the accuracy is : 0.9273\n",
      "in iteration the update is 0.000115332987the accuracy is : 0.9268\n",
      "in iteration the update is 0.0001095662588the accuracy is : 0.9269\n",
      "in iteration the update is 0.00010408793689the accuracy is : 0.9264\n",
      "in iteration the update is 9.888354e-0590the accuracy is : 0.9273\n",
      "in iteration the update is 9.393936e-0591the accuracy is : 0.9242\n",
      "in iteration the update is 8.9242385e-0592the accuracy is : 0.9273\n",
      "in iteration the update is 8.4780266e-0593the accuracy is : 0.9281\n",
      "in iteration the update is 8.054125e-0594the accuracy is : 0.9297\n",
      "in iteration the update is 7.651419e-0595the accuracy is : 0.9295\n",
      "in iteration the update is 7.2688475e-0596the accuracy is : 0.9263\n",
      "in iteration the update is 6.905405e-0597the accuracy is : 0.9283\n",
      "in iteration the update is 6.560134e-0598the accuracy is : 0.9298\n",
      "in iteration the update is 6.2321276e-0599the accuracy is : 0.9237\n",
      "in iteration the update is 5.9205213e-05100the accuracy is : 0.9279\n",
      "in iteration the update is 5.6244953e-05101the accuracy is : 0.9284\n",
      "in iteration the update is 5.3432705e-05102the accuracy is : 0.9282\n",
      "in iteration the update is 5.0761068e-05103the accuracy is : 0.9294\n",
      "in iteration the update is 4.8223013e-05104the accuracy is : 0.9277\n",
      "in iteration the update is 4.5811863e-05105the accuracy is : 0.9252\n",
      "in iteration the update is 4.3521268e-05106the accuracy is : 0.9281\n",
      "in iteration the update is 4.1345203e-05107the accuracy is : 0.9278\n",
      "in iteration the update is 3.9277944e-05108the accuracy is : 0.9266\n",
      "in iteration the update is 3.7314046e-05109the accuracy is : 0.9285\n",
      "in iteration the update is 3.5448345e-05110the accuracy is : 0.9274\n",
      "in iteration the update is 3.367593e-05111the accuracy is : 0.9256\n",
      "in iteration the update is 3.199213e-05112the accuracy is : 0.9285\n",
      "in iteration the update is 3.0392524e-05113the accuracy is : 0.9274\n",
      "in iteration the update is 2.8872897e-05114the accuracy is : 0.929\n",
      "in iteration the update is 2.7429252e-05115the accuracy is : 0.9262\n",
      "in iteration the update is 2.6057789e-05116the accuracy is : 0.9274\n",
      "in iteration the update is 2.47549e-05117the accuracy is : 0.9286\n",
      "in iteration the update is 2.3517154e-05118the accuracy is : 0.9268\n",
      "in iteration the update is 2.2341295e-05119the accuracy is : 0.9253\n",
      "in iteration the update is 2.122423e-05120the accuracy is : 0.9261\n",
      "in iteration the update is 2.016302e-05121the accuracy is : 0.9285\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "in iteration the update is 1.9154868e-05122the accuracy is : 0.9275\n",
      "in iteration the update is 1.8197125e-05123the accuracy is : 0.9271\n",
      "in iteration the update is 1.7287268e-05124the accuracy is : 0.9276\n",
      "in iteration the update is 1.6422904e-05125the accuracy is : 0.9254\n",
      "in iteration the update is 1.560176e-05126the accuracy is : 0.9267\n",
      "in iteration the update is 1.4821671e-05127the accuracy is : 0.9271\n",
      "in iteration the update is 1.4080588e-05128the accuracy is : 0.9278\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-4-a457fbfa21c0>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     79\u001b[0m         \u001b[1;32mfor\u001b[0m \u001b[0mbatch\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbatch_n\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     80\u001b[0m             \u001b[0mbatch_x\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbatch_y\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mmnist\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtrain\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnext_batch\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbatch_size\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 81\u001b[1;33m             \u001b[0msession\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtrain\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfeed_dict\u001b[0m\u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mbatch_x\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mbatch_y\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# 此处是最小化\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     82\u001b[0m         \u001b[0mcorr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0msession\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcorrection\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfeed_dict\u001b[0m\u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mmnist\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtest\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mimages\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mmnist\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtest\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlabels\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# 基于测试集对准确率进行测试\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     83\u001b[0m         \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"in iteration the update is \"\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msession\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlearning_rate\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstep\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;34m\"the accuracy is : \"\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcorr\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# 打印准确率\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32md:\\python\\lib\\site-packages\\tensorflow\\python\\client\\session.py\u001b[0m in \u001b[0;36mrun\u001b[1;34m(self, fetches, feed_dict, options, run_metadata)\u001b[0m\n\u001b[0;32m    776\u001b[0m     \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    777\u001b[0m       result = self._run(None, fetches, feed_dict, options_ptr,\n\u001b[1;32m--> 778\u001b[1;33m                          run_metadata_ptr)\n\u001b[0m\u001b[0;32m    779\u001b[0m       \u001b[1;32mif\u001b[0m \u001b[0mrun_metadata\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    780\u001b[0m         \u001b[0mproto_data\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtf_session\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mTF_GetBuffer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrun_metadata_ptr\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32md:\\python\\lib\\site-packages\\tensorflow\\python\\client\\session.py\u001b[0m in \u001b[0;36m_run\u001b[1;34m(self, handle, fetches, feed_dict, options, run_metadata)\u001b[0m\n\u001b[0;32m    980\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mfinal_fetches\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mfinal_targets\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    981\u001b[0m       results = self._do_run(handle, final_targets, final_fetches,\n\u001b[1;32m--> 982\u001b[1;33m                              feed_dict_string, options, run_metadata)\n\u001b[0m\u001b[0;32m    983\u001b[0m     \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    984\u001b[0m       \u001b[0mresults\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32md:\\python\\lib\\site-packages\\tensorflow\\python\\client\\session.py\u001b[0m in \u001b[0;36m_do_run\u001b[1;34m(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)\u001b[0m\n\u001b[0;32m   1030\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mhandle\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1031\u001b[0m       return self._do_call(_run_fn, self._session, feed_dict, fetch_list,\n\u001b[1;32m-> 1032\u001b[1;33m                            target_list, options, run_metadata)\n\u001b[0m\u001b[0;32m   1033\u001b[0m     \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1034\u001b[0m       return self._do_call(_prun_fn, self._session, handle, feed_dict,\n",
      "\u001b[1;32md:\\python\\lib\\site-packages\\tensorflow\\python\\client\\session.py\u001b[0m in \u001b[0;36m_do_call\u001b[1;34m(self, fn, *args)\u001b[0m\n\u001b[0;32m   1037\u001b[0m   \u001b[1;32mdef\u001b[0m \u001b[0m_do_call\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfn\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1038\u001b[0m     \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1039\u001b[1;33m       \u001b[1;32mreturn\u001b[0m \u001b[0mfn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1040\u001b[0m     \u001b[1;32mexcept\u001b[0m \u001b[0merrors\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mOpError\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1041\u001b[0m       \u001b[0mmessage\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcompat\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mas_text\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0me\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmessage\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32md:\\python\\lib\\site-packages\\tensorflow\\python\\client\\session.py\u001b[0m in \u001b[0;36m_run_fn\u001b[1;34m(session, feed_dict, fetch_list, target_list, options, run_metadata)\u001b[0m\n\u001b[0;32m   1019\u001b[0m         return tf_session.TF_Run(session, options,\n\u001b[0;32m   1020\u001b[0m                                  \u001b[0mfeed_dict\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfetch_list\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtarget_list\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1021\u001b[1;33m                                  status, run_metadata)\n\u001b[0m\u001b[0;32m   1022\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1023\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m_prun_fn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msession\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mhandle\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfeed_dict\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfetch_list\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "# 数据集介绍\n",
    "# MNIST数据集，100k的训练数据，10k的预测数据，数据由tensorflow中的examples.tutorials.mnist读取 \n",
    "# 数据集介绍：：Yann LeCun's website\n",
    "# 由28*28的像素组成输入特征，输出特征为0-9的数字\n",
    "\n",
    "# 可调节参数：\n",
    "# --------\n",
    "# batch_size, initial_weight,二次损失函数,learning_rate,epoch_n\n",
    "# --------\n",
    "\n",
    "import tensorflow as tf\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "\n",
    "mnist = input_data.read_data_sets(\"MNIST_data\", one_hot = True)\n",
    "\n",
    "# mini_batch的大小\n",
    "batch_size = 100\n",
    "batch_n = mnist.train.num_examples // batch_size\n",
    "\n",
    "# # 定义两个placeholder用来feed数据，分别代表x和y --784列和10列(one-hot)\n",
    "x = tf.placeholder(tf.float32, [None, 784])\n",
    "y = tf.placeholder(tf.float32, [None, 10])\n",
    "\n",
    "# # ----\n",
    "# # 构建多分类回归\n",
    "\n",
    "# # 定义weight和bias，初始化分别为正态随机和0.0\n",
    "# 第一层\n",
    "num_L1 = 2000\n",
    "keep_prob1 = 0.8\n",
    "weight_L1 = tf.Variable(tf.truncated_normal([784, num_L1], stddev = 0.1))\n",
    "bias_L1 = tf.Variable(tf.zeros([num_L1]) + 0.1)\n",
    "a_L1 = tf.matmul(x, weight_L1) + bias_L1\n",
    "z_L1 = tf.nn.tanh(a_L1)\n",
    "z_dropout_L1 = tf.nn.dropout(z_L1, keep_prob1)\n",
    "\n",
    "# 第二层\n",
    "num_L2 = 500\n",
    "keep_prob2 = 1.0\n",
    "weight_L2 = tf.Variable(tf.truncated_normal([num_L1, num_L2], stddev = 0.1))\n",
    "bias_L2 = tf.Variable(tf.zeros([num_L2]) + 0.1)\n",
    "a_L2 = tf.matmul(z_dropout_L1, weight_L2) + bias_L2\n",
    "z_L2 = tf.nn.tanh(a_L2)\n",
    "z_dropout_L2 = tf.nn.dropout(z_L2, keep_prob2)\n",
    "\n",
    "# 第三层\n",
    "num_L3 = 300\n",
    "keep_prob3 = 1.0\n",
    "weight_L3 = tf.Variable(tf.truncated_normal([num_L2, num_L3], stddev = 0.1))\n",
    "bias_L3 = tf.Variable(tf.zeros([num_L3]) + 0.1)\n",
    "a_L3 = tf.matmul(z_dropout_L2, weight_L3) + bias_L3\n",
    "z_L3 = tf.nn.tanh(a_L3)\n",
    "z_dropout_L3 = tf.nn.dropout(z_L3, keep_prob3)\n",
    "\n",
    "# 输出层\n",
    "num_y = 10\n",
    "weight_L4 = tf.Variable(tf.truncated_normal([num_L3, num_y], stddev = 0.1))\n",
    "bias_L4 = tf.Variable(tf.zeros([num_y]) + 0.1)\n",
    "a = tf.matmul(z_dropout_L3, weight_L4) + bias_L4\n",
    "y_head = tf.nn.softmax(a)\n",
    "\n",
    "# # 定义二次损失函数并依据梯度下降法进行训练 -- 这样梯度下降的train就变成了x和y的函数\n",
    "learning_rate = tf.Variable(0.01)\n",
    "update = tf.assign(learning_rate, learning_rate * 0.95)\n",
    "loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y, logits = y_head))\n",
    "optimizer = tf.train.GradientDescentOptimizer(learning_rate)\n",
    "train = optimizer.minimize(loss)\n",
    "\n",
    "init = tf.global_variables_initializer()\n",
    "\n",
    "correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_head, 1)) # tf.argmax找到x中等于1的最大的id\n",
    "correction = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # tf.cast 转换类型，将bool转为float，从而求得准确率\n",
    "\n",
    "# 迭代500次，进行mini_batch梯度下降\n",
    "epoch_n = 500\n",
    "with tf.Session() as session:\n",
    "    session.run(init)\n",
    "    for step in range(epoch_n):\n",
    "        for batch in range(batch_n):\n",
    "            batch_x, batch_y = mnist.train.next_batch(batch_size)\n",
    "            session.run(train, feed_dict= {x: batch_x, y: batch_y}) # 此处是最小化\n",
    "        corr = session.run(correction, feed_dict= {x: mnist.test.images, y: mnist.test.labels}) # 基于测试集对准确率进行测试\n",
    "        print(\"in iteration \" + str(step) + \" the learning rate is \" + str(session.run(learning_rate)) + \"the accuracy is : \" + str(corr)) # 打印准确率\n",
    "        session.run(update)\n",
    "# 这里看似有问题，其实没问题，因为图没变，DAG对输入的batch依次执行梯度下降法，\n",
    "# 并执行epoch_n个周期，权重会更新epoch_n * batch_n次\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
